package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/metadata"
	"learn_grpc/5_grpc_metadata/proto"
)

func main() {
	// 建立连接 grpc.WithTransportCredentials(insecure.NewCredentials())跳过证书验证
	conn, err := grpc.Dial("127.0.0.1:8080", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}
	defer conn.Close() //需要关闭连接，
	// 初始化客户端
	client1 := proto.NewGreeterClient(conn)
	resq := proto.HelloRequest{
		Name: "John",
	}
	// 初始化一个metadata数据
	//md := metadata.Pairs("timestamp", time.Now().Format(timestampFormat))
	md := metadata.New(map[string]string{
		"name":    "bobby",
		"pasword": "imooc",
	})
	//生成ctx，才用NewOutgoingContext
	ctx := metadata.NewOutgoingContext(context.Background(), md)
	//发出请求，并传递ctx到服务端
	respon, err := client1.SayHello(ctx, &resq)
	if err != nil {
		panic(err)
	}
	fmt.Println(respon.Message)
}
